package org.jeecg.modules.demo.zhidingzhouqigongzuo.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.peihebumenbiao.entity.Peihebumenbiao;
import org.jeecg.modules.demo.peihebumenbiao.service.IPeihebumenbiaoService;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.entity.ZhidingListDto;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.entity.ZhidingzhouqiDto;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.entity.Zhidingzhouqigongzuo;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.service.IZhidingzhouqigongzuoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserDepart;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.service.ISysUserDepartService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
 /**
 * @Description: 制定周期性工作
 * @Author: jeecg-boot
 * @Date:   2025-10-09
 * @Version: V1.0
 */
@Tag(name="制定周期性工作")
@RestController
@RequestMapping("/zhidingzhouqigongzuo/zhidingzhouqigongzuo")
@Slf4j
public class ZhidingzhouqigongzuoController extends JeecgController<Zhidingzhouqigongzuo, IZhidingzhouqigongzuoService> {
	@Autowired
	private IZhidingzhouqigongzuoService zhidingzhouqigongzuoService;
	@Autowired
	private ISysUserDepartService sysUserDepartService;
	@Autowired
	private ISysUserRoleService sysUserRoleService;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private IPeihebumenbiaoService peihebumenbiaoService;
	/**
	 * 分页列表查询
	 *
	 * @param zhidingzhouqigongzuo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "制定周期性工作-分页列表查询")
//	@Operation(summary="制定周期性工作-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<IPage<Zhidingzhouqigongzuo>> queryPageList(Zhidingzhouqigongzuo zhidingzhouqigongzuo,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//
//
//        QueryWrapper<Zhidingzhouqigongzuo> queryWrapper = QueryGenerator.initQueryWrapper(zhidingzhouqigongzuo, req.getParameterMap());
//		Page<Zhidingzhouqigongzuo> page = new Page<Zhidingzhouqigongzuo>(pageNo, pageSize);
//		IPage<Zhidingzhouqigongzuo> pageList = zhidingzhouqigongzuoService.page(page, queryWrapper);
//		return Result.OK(pageList);
//	}

	//只展示自己填的数据
	@GetMapping(value = "/list")
	public Result<IPage<ZhidingzhouqiDto>> list(Zhidingzhouqigongzuo zhidingzhouqigongzuo,
												@RequestParam(name="pageNo",defaultValue="1") Integer pageNo,
												@RequestParam(name="pageName",defaultValue = "10") Integer pageSize,
												HttpServletRequest  req){
		//获取登录用户的部门id
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String departId = sysUser.getDepartIds();
		//根据部门id找到该部门对应的全部数据表
		IPage<ZhidingzhouqiDto> pagedto = new Page<>();
		Page<Zhidingzhouqigongzuo> page = new Page<Zhidingzhouqigongzuo>();
		QueryWrapper<Zhidingzhouqigongzuo> queryWrapper = new QueryWrapper<>();
		IPage<Zhidingzhouqigongzuo> pageList = zhidingzhouqigongzuoService.page(page, queryWrapper);
		List<Zhidingzhouqigongzuo> list = pageList.getRecords();
		List<ZhidingzhouqiDto> listdto = new ArrayList<>();
		//存入IPage<ZhidingzhouqiDto>中
		for(int i = 0;i<list.size();i++){
			ZhidingzhouqiDto zhidingzhouqiDto = new ZhidingzhouqiDto();
			BeanUtils.copyProperties(list.get(i), zhidingzhouqiDto);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String formattedDate = sdf.format(list.get(i).getXiacihuibaoshijian());
			zhidingzhouqiDto.setXiacihuibaoshijian(formattedDate);
			//插入配合部门id和驳回情况中状态为驳回的部门id
			String peiheid = "";
			String bohuiqingkuang = "";
			QueryWrapper<Peihebumenbiao> queryWrapper2 = new QueryWrapper<>();
			queryWrapper2.eq("zhidingbiaoid", zhidingzhouqiDto.getId());
			List<Peihebumenbiao> list2 = peihebumenbiaoService.list(queryWrapper2);
			for(int j = 0;j<list2.size();j++){
				peiheid = peiheid+list2.get(j).getPeihebumenid()+",";
				if(list2.get(j).getZhuangtai().equals("驳回")){
					bohuiqingkuang = bohuiqingkuang+list2.get(j).getPeihebumenid()+",";
				}
			}
			//判断主管配合
			if(list.get(i).getZhuguanbumenid().equals(departId)){
				zhidingzhouqiDto.setZhuguanpeihe("我主管的");
			}else{
				zhidingzhouqiDto.setZhuguanpeihe("我配合的");
			}
			//去除最后一个字符串
			if(peiheid.length()>0){
				zhidingzhouqiDto.setPeihebumen(peiheid.substring(0, peiheid.length()-1));
			}
			if(bohuiqingkuang.length()>0){
				zhidingzhouqiDto.setBohuiqingkuang(bohuiqingkuang.substring(0, bohuiqingkuang.length()-1));
			}
			listdto.add(zhidingzhouqiDto);
		}
		pagedto.setRecords(listdto);
		return Result.OK(pagedto);
	}

	
	/**
	 *   添加(需要修改，为实现添加的配合部门)
	 *
	 * @param zhidingListDto
	 * @return
	 */
	@AutoLog(value = "制定周期性工作-添加")
	@Operation(summary="制定周期性工作-添加")
	@RequiresPermissions("zhidingzhouqigongzuo:y_zhidingzhouqigongzuo:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ZhidingListDto zhidingListDto) {
		Zhidingzhouqigongzuo zhidingzhouqigongzuo = new Zhidingzhouqigongzuo();
		BeanUtils.copyProperties(zhidingListDto, zhidingzhouqigongzuo);
		zhidingzhouqigongzuoService.save(zhidingzhouqigongzuo);
		return Result.OK("添加成功！");
	}

	//选择责任主管
	@GetMapping(value="/zerenzhuguanList")
	public Result<List<SysUser>> zerenzhuguanList(){
		List<SysUser> list =zhidingzhouqigongzuoService.getZerenzhuguanid();
		return Result.OK(list);
	}

	//获取配合部门
	@GetMapping(value="/peiheList")
	public Result<List<SysDepart>> peiheList(){
		List<SysDepart> list = zhidingzhouqigongzuoService.getPeihebumen();
		return Result.OK(list);
	}
	/**
	 *  编辑
	 *
	 * @param zhidingzhouqigongzuo
	 * @return
	 */
	@AutoLog(value = "制定周期性工作-编辑")
	@Operation(summary="制定周期性工作-编辑")
	@RequiresPermissions("zhidingzhouqigongzuo:y_zhidingzhouqigongzuo:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Zhidingzhouqigongzuo zhidingzhouqigongzuo) {
		zhidingzhouqigongzuoService.updateById(zhidingzhouqigongzuo);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "制定周期性工作-通过id删除")
	@Operation(summary="制定周期性工作-通过id删除")
	@RequiresPermissions("zhidingzhouqigongzuo:y_zhidingzhouqigongzuo:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		zhidingzhouqigongzuoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "制定周期性工作-批量删除")
	@Operation(summary="制定周期性工作-批量删除")
	@RequiresPermissions("zhidingzhouqigongzuo:y_zhidingzhouqigongzuo:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.zhidingzhouqigongzuoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "制定周期性工作-通过id查询")
	@Operation(summary="制定周期性工作-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Zhidingzhouqigongzuo> queryById(@RequestParam(name="id",required=true) String id) {
		Zhidingzhouqigongzuo zhidingzhouqigongzuo = zhidingzhouqigongzuoService.getById(id);
		if(zhidingzhouqigongzuo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(zhidingzhouqigongzuo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param zhidingzhouqigongzuo
    */
    @RequiresPermissions("zhidingzhouqigongzuo:y_zhidingzhouqigongzuo:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Zhidingzhouqigongzuo zhidingzhouqigongzuo) {
        return super.exportXls(request, zhidingzhouqigongzuo, Zhidingzhouqigongzuo.class, "制定周期性工作");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("zhidingzhouqigongzuo:y_zhidingzhouqigongzuo:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Zhidingzhouqigongzuo.class);
    }

}
